﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using TrackIT.Common;
using TrackIT.Common.Providers;
using TrackIT.Common.Repositories;

namespace TrackIT.UnitTest.Repositories
{
    [TestClass]
    public class WorkLogRepositoryTest
    {
        private IUnityContainer _container;
        private Mock<IFileChangeNotifyProvider> _fileChangeNotifyProvider;

        [TestInitialize]
        public void TestInitialize()
        {
            _fileChangeNotifyProvider = new Mock<IFileChangeNotifyProvider>();

            _container = new UnityContainer()
                .RegisterType<IFileAccessRepository, MockFileAccessRepository>()
                .RegisterType<IDateTimeProvider, MockDateTimeProvider>()
                .RegisterType<IWorkLogRepository, WorkLogRepository>()
                .RegisterInstance(typeof(IFileChangeNotifyProvider), _fileChangeNotifyProvider.Object)
                .RegisterInstance(typeof(string), "TaskPrefix", "EOL_")
                .RegisterInstance(typeof(string), "WorkFilename", @"c:\temp\workfile.txt")
                .RegisterInstance(typeof(bool), "DetectWorkFileChange", false); 
        }

        [TestCleanup]
        public void TestCleanup()
        {
            _container.Dispose();
        }

        [TestMethod]
        public void Ctor_ValidFile_IsNotNull()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListValid1;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            Assert.IsNotNull(target);
        }

        [TestMethod]
        public void Load_ValidFile_CorrectNumberOfTasksLoaded()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListValid1;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();
            
            Assert.AreEqual(3, target.Tasks.Count());
            Assert.AreEqual(5, target.WorkItems.Count());
        }

        [TestMethod]
        public void Load_DuplicateTasks_CorrectNumberOfTasksLoaded()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListMultipleTasks;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual(3, target.Tasks.Count());
            Assert.AreEqual(7, target.WorkItems.Count());
        }

        [TestMethod]
        public void Load_EmptyFile_CorrectNumberOfTasksLoaded()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual(0, target.Tasks.Count());
            Assert.AreEqual(0, target.WorkItems.Count());
        }

        [TestMethod]
        public void Load_ValidFile_GetLastWorkEntryIsCorrect()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            TrackITBaseConfigurationSection config = new MockTaskConfigSection();
            fileAccessRepository.Items = HelperUtil.WorkListMultipleTasks;
            config.TaskPrefix = "EOL_";
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);
            _container.RegisterInstance(typeof(TrackITBaseConfigurationSection), config);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            WorkEntry actual = target.GetLast();
            Assert.AreEqual("61703", actual.Task);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void Load_InvalidFileNoDateMarker_ExceptionThrown()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            List<string> workEntries = HelperUtil.WorkEntryStringToList("% EOL_12345 13:56");
            fileAccessRepository.Items = workEntries;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();
        }

        [TestMethod]
        public void Start_NewFile_TaskIsStarted()
        {
            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();
            WorkEntry actual = target.Start("StartTask");

            Assert.AreEqual("StartTask", actual.Task);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void Start_TaskAlreadyStarted_ExceptionThrown()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListStartedTask1;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();
            target.Start("StartTask");
        }
        
        [TestMethod]
        public void Start_PreviousTaskWasYesterday_AddDateMarkerBeforeStartingNewTask()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            fileAccessRepository.Items = HelperUtil.WorkEntryStringToList("2012-03-22\n\r% EOL_12345 13:56 17:30");
            dateTimeProvider.Current = new DateTime(2012, 03, 23);
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);
            _container.RegisterInstance(typeof(IDateTimeProvider), dateTimeProvider);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();
            target.Start("StartTask");

            Assert.AreEqual(4, target.WorkItems.Count());
            Assert.AreEqual(dateTimeProvider.Current, target.WorkItems.Last(w => w.EntryType == WorkEntryType.Date).Start);
        }

        [TestMethod]
        public void Stop_TaskAlreadyStarted_TaskStopped()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListStartedTask1;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();
            WorkEntry actual = target.Stop();

            Assert.AreEqual("StartTask", actual.Task);
            Assert.IsNotNull(actual.Start);
            Assert.IsNotNull(actual.Stop);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void Stop_TaskNotStarted_ExceptionThrow()
        {
            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();
            target.Stop();
        }
        
        [TestMethod]
        public void Save_EmptyWorkList_NothingSaved()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Save();

            Assert.AreEqual(0, fileAccessRepository.Items.Count);
        }

        [TestMethod]
        public void Save_WorkListLoaded_Saved()
        {
            TrackITBaseConfigurationSection config = new MockTaskConfigSection();
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListValid1;
            config.TaskPrefix = "EOL_";
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);
            _container.RegisterInstance(typeof(TrackITBaseConfigurationSection), config);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            fileAccessRepository.Clear();
            target.Save();

            Assert.AreEqual(5, fileAccessRepository.Items.Count());
            Assert.AreEqual("% 2012-04-19", fileAccessRepository.Items[0]);
            Assert.AreEqual("% Hardening 09:45 17:00", fileAccessRepository.Items[1]);
            Assert.AreEqual("% 2012-04-23", fileAccessRepository.Items[2]);
            Assert.AreEqual("% EOL_61579 10:14 11:22", fileAccessRepository.Items[3]);
            Assert.AreEqual("% EOL_61703 11:22 13:17", fileAccessRepository.Items[4]);
        }

        [TestMethod]
        public void Save_StartedTaskNonMantis_Saved()
        {
            TrackITBaseConfigurationSection config = new MockTaskConfigSection();
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListStartedTask1;
            config.TaskPrefix = "EOL_";
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);
            _container.RegisterInstance(typeof(TrackITBaseConfigurationSection), config);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            fileAccessRepository.Clear();
            target.Save();

            Assert.AreEqual(2, target.WorkItems.Count());
            WorkEntry first = target.WorkItems.First();
            Assert.AreEqual(WorkEntryType.Date, first.EntryType);
            Assert.AreEqual(new DateTime(2011, 3, 2), first.Start);

            WorkEntry second = target.WorkItems.Skip(1).First();
            Assert.AreEqual("StartTask", second.Task);
            Assert.IsFalse(second.IsMantisTicket);
            Assert.AreEqual(new DateTime(2011, 3, 2, 13, 56, 0), second.Start);
            Assert.IsNull(second.Stop);
        }

        [TestMethod]
        public void Save_StartedTaskMantis_Saved()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListStartedTask2;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            fileAccessRepository.Clear();
            target.Save();

            Assert.AreEqual(2, target.WorkItems.Count());
            WorkEntry first = target.WorkItems.First();            
            Assert.AreEqual(WorkEntryType.Date, first.EntryType);
            Assert.AreEqual(new DateTime(2012, 11, 20), first.Start);

            WorkEntry second = target.WorkItems.Skip(1).First();
            Assert.AreEqual("12345", second.Task);
            Assert.IsTrue(second.IsMantisTicket);
            Assert.AreEqual(new DateTime(2012, 11, 20, 13, 56, 0), second.Start);
            Assert.IsNull(second.Stop);
        }

        [TestMethod]
        public void Task_LongTaskList_CorrectNumberOfTasksLoaded()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListLongList;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual(28, target.Tasks.Count());
        }
        
        [TestMethod]
        public void Task_SingleMantisTaskList_TaskLoadedCorrectly()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListStartedTask2;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual(1, target.Tasks.Count());
            Assert.AreEqual("12345", target.Tasks.First().Name);
        }

        [TestMethod]
        public void Load_DateAndTask_EnsureTheTaskDatesAreCorrect()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            const string workLog = "% 2012-08-21\n\r% EOL_61707 14:56 15:04";
            fileAccessRepository.Items = HelperUtil.WorkEntryStringToList(workLog);
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual(2, target.WorkItems.Count());
            WorkEntry workEntry = target.WorkItems.First(w => w.EntryType != WorkEntryType.Date);
            Assert.AreEqual(new DateTime(2012, 8, 21, 14, 56, 0), workEntry.Start);
            Assert.AreEqual(new DateTime(2012, 8, 21, 15, 04, 0), workEntry.Stop);
        }

        [TestMethod]
        public void Task_SingleNonMantisTaskList_TaskLoadedCorrectly()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            fileAccessRepository.Items = HelperUtil.WorkListStartedTask1;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual(1, target.Tasks.Count());
            Assert.AreEqual("StartTask", target.Tasks.First().Name);
            Assert.AreEqual("StartTask", target.Current.Task);
        }

        [TestMethod]
        public void GetWorkSummary_SingleEntryInRange1_SingleSummaryWithCorrectTime1()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            const string workLog = "% 2012-01-01\n\r% EOL_61707 14:56 15:04";
            fileAccessRepository.Items = HelperUtil.WorkEntryStringToList(workLog);
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            DateTime start = new DateTime(2012, 01, 01);
            DateTime end = new DateTime(2012, 01, 31);
            FilterDates filterDates = new FilterDates() { StartDate = start, StopDate = end };
            
            List<string> actual = target.GetWorkSummary(filterDates);
            Assert.AreEqual(2, actual.Count);
            Assert.AreEqual("EOL issues allocations", actual[0]);
            Assert.AreEqual("61707 0.13", actual[1]);
        }

        [TestMethod]
        public void GetNextActionType_LastEntryStop_NextIsStart()
        {
            TrackITBaseConfigurationSection config = new MockTaskConfigSection();
            MockFileAccessRepository fileAccess = new MockFileAccessRepository();
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            const string workLog = "% 2012-01-01\n\r% EOL_61707 14:56 15:04";
            fileAccess.Items = HelperUtil.WorkEntryStringToList(workLog);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual(WorkEntryType.Start, target.GetNextActionType());
        }

        [TestMethod]
        public void GetNextActionType_LastEntryStart_NextIsStop()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            const string workLog = "% 2012-01-01\n\r% EOL_61707 14:56";
            fileAccessRepository.Items = HelperUtil.WorkEntryStringToList(workLog);
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual(WorkEntryType.Stop, target.GetNextActionType());
        }

        [TestMethod]
        public void GetNextActionType_NoEntries_NextIsStart()
        {
            TrackITBaseConfigurationSection config = new MockTaskConfigSection();
            MockFileAccessRepository fileAccess = new MockFileAccessRepository();
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            const string workLog = "% 2012-01-01";
            fileAccess.Items = HelperUtil.WorkEntryStringToList(workLog);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual(WorkEntryType.Start, target.GetNextActionType());
        }

        [TestMethod]
        public void GetTaskByName_MantisTask_FindsCorrectTask()
        {
            TrackITBaseConfigurationSection config = new MockTaskConfigSection();
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            const string workLog = "% 2012-01-01\n\r% EOL_03452 14:56";
            fileAccessRepository.Items = HelperUtil.WorkEntryStringToList(workLog);
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual("3452", target.GetTaskByName("3452").Name);
        }


        [TestMethod]
        public void GetTaskByName_NonMantisTask_FindsCorrectTask()
        {
            TrackITBaseConfigurationSection config = new MockTaskConfigSection();
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            fileAccessRepository.Items = HelperUtil.WorkListLongList;
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual("REVIEW", target.GetTaskByName("REVIEW").Name);
        }

        [TestMethod]
        public void GetWorkSummary_SingleEntryInRang2e_SingleSummaryWithCorrectTime2()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            const string workLog = "% 2012-01-01\n\r% EOL_61975 15:04 17:45";
            fileAccessRepository.Items = HelperUtil.WorkEntryStringToList(workLog);
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            DateTime start = new DateTime(2012, 01, 01);
            DateTime end = new DateTime(2012, 01, 31);
            FilterDates filterDates = new FilterDates() { StartDate = start, StopDate = end };
            List<string> actual = target.GetWorkSummary(filterDates);
            Assert.AreEqual(2, actual.Count);
            Assert.AreEqual("EOL issues allocations", actual[0]);
            Assert.AreEqual("61975 2.68", actual[1]);
        }

        [TestMethod]
        public void GetWorkSummary_MultipleEntriesInRange1_SingleSummaryWithCorrectTime1()
        {
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            const string workLog = "% 2012-01-01\n\r% EOL_61707 14:56 15:04\n\r% EOL_61707 15:56 17:14";
            fileAccessRepository.Items = HelperUtil.WorkEntryStringToList(workLog);
            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            DateTime start = new DateTime(2012, 01, 01);
            DateTime end = new DateTime(2012, 01, 31);
            FilterDates filterDates = new FilterDates() { StartDate = start, StopDate = end };
            List<string> actual = target.GetWorkSummary(filterDates);
            Assert.AreEqual(2, actual.Count);
            Assert.AreEqual("EOL issues allocations", actual[0]);
            Assert.AreEqual("61707 1.43", actual[1]);
        }

        [TestMethod]
        public void GetTimeSpentOnTask_MultipleEntries_CorrectDurationReturned()
        {
            TrackITBaseConfigurationSection config = new MockTaskConfigSection();
            MockFileAccessRepository fileAccessRepository = new MockFileAccessRepository();
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            const string workLog = "% 2012-01-01\n\r% EOL_61707 14:56 15:04\n\r% EOL_61707 15:56 17:14";
            fileAccessRepository.Items = HelperUtil.WorkEntryStringToList(workLog);

            _container.RegisterInstance(typeof(IFileAccessRepository), fileAccessRepository);
            _container.RegisterInstance(typeof(IDateTimeProvider), dateTimeProvider);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            target.Load();

            Assert.AreEqual(new TimeSpan(1, 26, 0), target.GetTimeSpentOnTask("61707"));
        }

        [TestMethod]
        public void GetTemporaryFolderName_Weekly_CorrectFilename()
        {
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            dateTimeProvider.Current = new DateTime(2012, 5, 11);
            _container.RegisterInstance(typeof(IDateTimeProvider), dateTimeProvider);
            
            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();

            string filename = target.GetTemporaryFolderName(ReportType.Weekly);
            Assert.IsTrue(filename.EndsWith(@"2012-05 Wk2 Summary.txt"));
        }

        [TestMethod]
        public void GetTemporaryFolderName_Monthly_CorrectFilename()
        {
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            dateTimeProvider.Current = new DateTime(2012, 5, 11);
            _container.RegisterInstance(typeof(IDateTimeProvider), dateTimeProvider);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            string filename = target.GetTemporaryFolderName(ReportType.Monthly);
            Assert.IsTrue(filename.EndsWith(@"2012-05 Summary.txt"));
        }

        [TestMethod]
        public void ResolveDatesFromReportType_WeeklyEndOfWeek_ReturnCurrentWeek()
        {         
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            dateTimeProvider.Current = new DateTime(2012, 5, 11);
            _container.RegisterInstance(typeof(IDateTimeProvider), dateTimeProvider);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            FilterDates actual = target.ResolveDatesFromReportType(ReportType.Weekly);

            Assert.AreEqual(new DateTime(2012, 5, 7), actual.StartDate);
            Assert.AreEqual(new DateTime(2012, 5, 13), actual.StopDate);
        }
        
        [TestMethod]
        public void ResolveDatesFromReportType_WeeklyMidweek_ReturnLastWeek()
        {
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            dateTimeProvider.Current = new DateTime(2012, 5, 17);
            _container.RegisterInstance(typeof(IDateTimeProvider), dateTimeProvider);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            FilterDates actual = target.ResolveDatesFromReportType(ReportType.Weekly);

            Assert.AreEqual(new DateTime(2012, 5, 7), actual.StartDate);
            Assert.AreEqual(new DateTime(2012, 5, 13), actual.StopDate);
        }

        [TestMethod]
        public void ResolveDatesFromReportType_WeeklyStartOfWeek_ReturnLastWeek()
        {
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            dateTimeProvider.Current = new DateTime(2012, 5, 14);
            _container.RegisterInstance(typeof(IDateTimeProvider), dateTimeProvider);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            FilterDates actual = target.ResolveDatesFromReportType(ReportType.Weekly);

            Assert.AreEqual(new DateTime(2012, 5, 7), actual.StartDate);
            Assert.AreEqual(new DateTime(2012, 5, 13), actual.StopDate);
        }
        
        [TestMethod]
        public void ResolveDatesFromReportType_MonthlyStartOfMonth_ReturnLastMonth()
        {
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            dateTimeProvider.Current = new DateTime(2012, 5, 1);
            _container.RegisterInstance(typeof(IDateTimeProvider), dateTimeProvider);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            FilterDates actual = target.ResolveDatesFromReportType(ReportType.Monthly);

            Assert.AreEqual(new DateTime(2012, 4, 1), actual.StartDate);
            Assert.AreEqual(new DateTime(2012, 4, 30), actual.StopDate);
        }

        [TestMethod]
        public void ResolveDatesFromReportType_Monthly21st_ReturnLastMonth()
        {
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            dateTimeProvider.Current = new DateTime(2012, 5, 21);
            _container.RegisterInstance(typeof(IDateTimeProvider), dateTimeProvider);
            
            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            FilterDates actual = target.ResolveDatesFromReportType(ReportType.Monthly);

            Assert.AreEqual(new DateTime(2012, 4, 1), actual.StartDate);
            Assert.AreEqual(new DateTime(2012, 4, 30), actual.StopDate);
        }

        [TestMethod]
        public void ResolveDatesFromReportType_Monthly22nd_ReturnCurrentMonth()
        {
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            dateTimeProvider.Current = new DateTime(2012, 5, 22);
            _container.RegisterInstance(typeof(IDateTimeProvider), dateTimeProvider);
            
            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();
            FilterDates actual = target.ResolveDatesFromReportType(ReportType.Monthly);

            Assert.AreEqual(new DateTime(2012, 5, 1), actual.StartDate);
            Assert.AreEqual(new DateTime(2012, 5, 31), actual.StopDate);
        }

        [TestMethod]
        public void GetTimeSpentOnTask_NewTaskNoPreviousEntries_NoExceptionZeroMinutesReturned()
        {
            MockDateTimeProvider dateTimeProvider = new MockDateTimeProvider();
            dateTimeProvider.Current = new DateTime(2012, 5, 11);

            IWorkLogRepository target = _container.Resolve<IWorkLogRepository>();

            TimeSpan actual = target.GetTimeSpentOnTask("NewTask");

            Assert.AreEqual(new TimeSpan(0), actual);
        }
    }
}
